package grape.common.rest.common;

import com.fasterxml.classmate.ResolvedType;
import com.fasterxml.classmate.TypeResolver;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.apache.commons.lang3.StringUtils;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.paths.RelativePathProvider;
import springfox.documentation.spring.web.plugins.Docket;

import javax.servlet.ServletContext;
import java.lang.annotation.Annotation;

/**
 * Created by yangwei
 * Created at 2020/8/4 13:13
 */
public interface SwaggerConfig {


    public static final String application_name_grape_portal_one = "grape-portal-one";


    public static final String parameter_type_query = "query";
    public static final String parameter_type_body = "body";
    public static final String parameter_type_form = "form";
    public static final String parameter_type_header = "header";
    public static final String parameter_type_cookie = "cookie";

    default ParameterBuilder parameterBuilder(){
        return new ParameterBuilder();
    }

    default Parameter authorization(){
        return parameterBuilder()
                .name("Authorization")
                .description("token,如：Bearer或Basic")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
    }
    default Docket docket(String grapeName){
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        if(grapeName != null){
            docket = docket.groupName(grapeName);
        }
        return docket;
    }
    default Docket docket(){
        return docket(null);
    }
    default ApiInfo apiInfo(String grapeName) {
        return new ApiInfoBuilder()
                //自定义信息可按需求填写
                .title("Grape "+ grapeName +" Swagger 接口文档")
                .build();
    }

    default Predicate<RequestHandler> basePackage(String basePackage){
        return RequestHandlerSelectors.basePackage(basePackage);
    }

    default Predicate<RequestHandler> withClassAnnotation(final Class<? extends Annotation> annotation){
        return RequestHandlerSelectors.withClassAnnotation(annotation);
    }
    default Predicate<RequestHandler> withMethodAnnotation(final Class<? extends Annotation> annotation){
        return RequestHandlerSelectors.withMethodAnnotation(annotation);
    }
    default Predicate<String> path(String path){
        return new Predicate<String>() {
            @Override
            public boolean apply(String input) {
                return StringUtils.contains(input, path);
            }
        };
    }
    default Predicate<String> notPath(String path){
        return Predicates.not(path(path));
    }

    default ModelRef modelRefString(){
        return new ModelRef("string");
    }

    default ResolvedType resolvedTypeString(){
        return new TypeResolver().resolve(String.class);
    }

    default RelativePathProvider relativePathProviderForBase(String basePath,String applicationName,ServletContext servletContext){
        String tempBasePath = "/api";
        if (!application_name_grape_portal_one.equals(applicationName)) {
            tempBasePath += basePath;
        }
        String finalTempBasePath = tempBasePath;
        return new RelativePathProvider(servletContext) {
            @Override
            public String getApplicationBasePath() {
                return finalTempBasePath;
            }
        };
    }
}
